<div class="layui-fluid layui-anim website-anim" id="website-template" lay-title="模板管理">
    <div class="layui-row website-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body website-table-full">
                    <form class="layui-form layui-table-form" lay-filter="template-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md8">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">模板名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="templateName" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">菜单名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="menuName" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md4">
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action" id="query">
                                    查询
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action" id="reset">
                                    重置
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action" shiro:hasAnyPermissions="template:add" id="templateAdd">
                                    新增
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="templateTable" lay-data="{id: 'templateTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="template-option">
    <span shiro:lacksPermission="template:list,template:update,template:delete">
        <span class="layui-badge-dot website-bg-orange"></span> 无权限
    </span>
    <a lay-event="detail" shiro:hasPermission="template:list" class="website-green">查看</a>
    <a lay-event="edit" shiro:hasPermission="template:update" class="website-blue">修改</a>
    <a lay-event="del" shiro:hasPermission="template:delete" class="website-red">删除</a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'form', 'table', 'website','dict'], function () {
        var $ = layui.jquery,
            website = layui.website,
            form = layui.form,
            table = layui.table,
            dict = layui.dict,
            $view = $('#website-template'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $templateAdd = $view.find('#templateAdd'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo;


        form.render();
        initTable();
        table.on('tool(templateTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {
                website.modal.view('详细信息', 'system/template/templateDetail/' + data.templateId, {
                    area: $(window).width() <= 750 ? '95%' : '660px'
                });
            }
            if (layEvent === 'del') {
                website.modal.confirm('删除信息', '确定删除该信息？', function () {
                    deleteExperts(data.templateId);
                });
            }
            if (layEvent === 'edit') {
                website.modal.open('修改信息', 'system/template/templateUpdate/' + data.templateId, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    offset: '30px',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#template-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        $('#template-update').find('#reset').trigger('click');
                        layer.closeAll();
                    }
                });
            }
        });
        table.on('sort(templateTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });
        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            createTimeTo = null;
            createTimeFrom = null;
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });
        $templateAdd.on('click', function () {
            website.modal.open('新增信息', 'system/template/templateAdd', {
                btn: ['提交', '重置'],
                area: $(window).width() <= 750 ? '95%' : '50%',
                offset: '30px',
                yes: function (index, layero) {
                    $('#template-add').find('#submit').trigger('click');
                },
                btn2: function () {
                    $('#template-add').find('#reset').trigger('click');
                    $('#template-add').find('#reset-form').trigger('click');

                    return false;
                }
            });
        });
        function initTable() {
            tableIns = website.table.init({
                elem: $view.find('table'),
                id: 'templateTable',
                url: ctx + 'template/list',
                cols: [[
                    { type: 'numbers', title: '序号',minWidth: 20},
                    {field: 'templateName', title: '模板名称',minWidth: 100 },
                    {field: 'menuName', title: '菜单名称',minWidth: 100 },
                    {field: 'fileName', title: '文件名称',minWidth: 200 },
                    {title: '操作', toolbar: '#template-option', minWidth: 200}
                ]]
            });
        }
        function getQueryParams() {
            var params = $searchForm.serializeJson();
            var createTime = params.time;
            if (createTime) {
                createTimeFrom = createTime.split(' - ')[0];
                createTimeTo = createTime.split(' - ')[1];
            }
            params.invalidate_ie_cache = new Date();
            params.createTimeFrom = createTimeFrom;
            params.createTimeTo = createTimeTo;
            return params;
        }
        function deleteExperts(templateId) {
            website.post(ctx + 'template/delete/' + templateId, null, function () {
                website.alert.success('删除成功');
                $query.click();
            });
        }
    })
</script>